package com.demo.utils;

import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

import java.util.Map;

public class JsonUtil {

    /**
     * @Description: read specified json file， the variable will be replaced if exist
     * @Parameters: jsonFileName, json file name
     * @Parameters: newValueMap, Map， new value
     * @Return: string
     **/
    public static String readJson(String jsonFileName, Map<String, Object> newValueMap) {
        String jsonFileUri = String.format("json/%s", jsonFileName);
        String jsonFilePath = JsonUtil.class.getClassLoader().getResource(jsonFileUri).getPath();
        JtwigTemplate template = JtwigTemplate.fileTemplate(jsonFilePath);
        JtwigModel model = JtwigModel.newModel(newValueMap);
        return template.render(model);
    }
}
